#include <stdio.h>
#include <string.h>
#include <dos.h>
#include "mpx_supt.h"

#define SYS_STACK_SIZE 200
byte sys_stack[SYS_STACK_SIZE];

unsigned short ss_save;
unsigned short sp_save;
unsigned short new_ss;
unsigned short new_sp;

unsinged char *MK_FP(unsigned int, unsigned int)
unsigned char FP_SEG(void*)
unsigned char FP_OFF(void*)

void interrupt name();
void interrupt dispatch();
void sys_call(void);
int sys_set_vec(void (*handler)());
void load_procs();

typedef struct context
{
	unsigned int BP, DI, SI, DS, ES;
	unsigned int DX, CX, BX, AX;
	unsigned int IP, CS, FLAGS;
} context;

context *context_p;

typedef struct params
{
	int op_code;
	int device_id;
	byte *buf_addr;
	int *count_addr;
} params;

params *param_p;

PCB *cop;

void interupt name()
{
	
}

void interrupt dispatch()
{
	if(sp_save == NULL)
	{
		ss_save = _SS;
		sp_save = _SP;

		cop = *PCB;
		if(PCB != NULL)
		{
			//set_SS = PCB Stack pointer;
			//set_SP = PCB Stack Pointer;
		}

		else
		{
			cop = NULL;
			set_SS = ss_save;
			set_SP = sp_save;
		}
	}
}

void sys_call()
{
	ss_save = _SS;
	sp_save = _SP;

	new_ss = FP_SEG(sys_stack);
	new_sp = FP_OFF(sys_stack);
	new_sp += SYS_STACK_SIZE;

	_SS = new_ss;
	_SP = new_sp;

	cop->stack_ptr = (unsigned char *)MK_FP(_SS, _SP):
	param_p = (params *)(cop->stack_ptr + sizeof(context);

	if(param_p->op_code = IDLE)
	{
		//move pcb to ready queue
		//insert pcb by priority
	}
	else if(param_p->op_code = EXIT)
	{
		//delete PCB;
		cop = NULL;
	}
	else
		contex_p->AX = op_code;

	dispatch();
}

int sys_set_vec(void *sys_call())
{
	if(op_code >= 0)
		return 0;

	else
		return ERR_SUP_INVHAN;
}

void load_procs() //temporary
{
	Pcb.stack_p = pcb.stack_base + pcb.stack_size - sizeof(context);
	context_p = (context*)pcb.stack_p;

	context_p->DS = _DS;
	context_p->ES = _ES;
	context_p->CS = FP_SEG(&testn_R3);
	context_p->IP = FP_OFF(&testn_R3);
	context_p->FLAGS = 0x200;

	int i = 0;
	while(i <= 4)
	{
		puts("Test %d dispatched.\n", i+1);
		sys_req(IDLE, NO_DEV, NULL, NULL);
	}
	while(i <= 4)
	{
		puts("Test %d complete.\n", i+1);
		sys_req(EXIT, NO_DEV, NULL, NULL);
		//display error message

		if(i = 4)
			puts("All tests completed.\n")
	}
	return;
}